package jchat.genral;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;

import jchat.interfaces.IAppManager;
import jchat.services.IMService;
import jchat.types.Session;
import chat.jdaas.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class Setstatus extends Activity {
	private IAppManager imService;
	private Handler handler = new Handler();
    String imagepath;
    ProgressDialog simpleWaitDialog;
	private EditText user_status;
	private ListView pre_stats;
	private Button select_pic,submit;
	private ImageView profile_viewpic;
	String name=null;
    String password=null;
    private String settingsTAG = "AppNameSettings";
	private SharedPreferences prefs;
	String[] status={"Busy","At School","At Office","Call you later"};
private ServiceConnection mConnection = new ServiceConnection() {
        

		public void onServiceConnected(ComponentName className, IBinder service) {
         
            imService = ((IMService.IMBinder)service).getService();  
         
      
            
        }

        public void onServiceDisconnected(ComponentName className) {
           
        	imService = null;
            //Toast.makeText(SignUp.this, R.string.local_service_stopped,
              //      Toast.LENGTH_SHORT).show();
        }
    };
    @Override
	protected void  onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.setstatus);
		user_status=(EditText)findViewById(R.id.user_status);
		pre_stats=(ListView)findViewById(R.id.predefind_status);
		select_pic=(Button)findViewById(R.id.selectimage);
		submit=(Button)findViewById(R.id.submit);
		profile_viewpic=(ImageView)findViewById(R.id.profile_pic);
		 prefs = getSharedPreferences(settingsTAG, 0);
         this.name = prefs.getString(Session.Username, "");
		this.password=prefs.getString(Session.PASSWORD, "");
		ArrayAdapter<String> adpter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,status);
		pre_stats.setAdapter(adpter);
		pre_stats.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String s=arg0.getItemAtPosition(arg2).toString();
				user_status.setText(s);
				Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
				
			}
		});
		submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new ImageUploaderTask().execute(imagepath);
			}
		});
		select_pic.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
	            intent.setType("image/*");
	            intent.setAction(Intent.ACTION_GET_CONTENT);
	            startActivityForResult(Intent.createChooser(intent, "Complete action using"), 1);
				Toast.makeText(getApplicationContext(), "cancle", Toast.LENGTH_LONG).show();
			}
		});
		
	}
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    
    	if (requestCode == 1 && resultCode == RESULT_OK) {
            //Bitmap photo = (Bitmap) data.getData().getPath(); 
          
            Uri selectedImageUri = data.getData();
            imagepath = getPath(selectedImageUri);
            Bitmap bitmap=BitmapFactory.decodeFile(imagepath);
            profile_viewpic.setImageBitmap(bitmap);
           // messageText.setText("Uploading file path:" +imagepath);
            
	    	
	    }
    }
    	 public String getPath(Uri uri) {
    	        String[] projection = { MediaStore.Images.Media.DATA };
    	        Cursor cursor = managedQuery(uri, projection, null, null, null);
    	        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    	        cursor.moveToFirst();
    	        return cursor.getString(column_index);
    	    }
	@Override
    protected void  onResume() {
		bindService(new Intent(Setstatus.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);
		super.onResume();
    	
		
	}
	@Override
	protected void onPause()
	{
		unbindService(mConnection);

		super.onPause();
	}
	
	
	
	
private class ImageUploaderTask extends AsyncTask<String, Integer, Void> {
	String conteh=null;
		@Override
		protected void onPreExecute(){
			simpleWaitDialog = ProgressDialog.show(Setstatus.this, "Wait", "Uploading Image");
		}
		
		@Override
		protected Void doInBackground(String... params) {
			
			conteh=new SetProfile().uploadSingleImage(params[0],Setstatus.this.name,Setstatus.this.password,user_status.getText().toString());
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result){
			//super.onPostExecute(result);
			simpleWaitDialog.dismiss();
			if(conteh.equals("OK"))
			{
				Intent ih=new Intent(Setstatus.this, FriendList.class);
				startActivity(ih);
				Setstatus.this.finish();
			}
			
			
		}
	}

}
